package com.nba.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nba.bean.Code;
import com.nba.service.AdminService;
import com.nba.service.CodeService;
import com.nba.service.impl.AdminServiceImpl;
import com.nba.service.impl.CodeServiceImpl;

@WebServlet("/regist.do")
public class RegistServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String name = req.getParameter("adminName");
		String password = req.getParameter("pwd");
		String codestr = req.getParameter("code");
		if(name == null) {
			req.getRequestDispatcher("regist.jsp").forward(req, resp);
		}
		AdminService adminservice = new AdminServiceImpl();
		Code code = (Code) req.getSession().getAttribute("code");
		
		int num = adminservice.selectAdmin(name);
		if(num==-1) {
			if(code.getCodeText().equals(codestr)) {
				adminservice.addAdmin(name, password);
				resp.sendRedirect("login.jsp");
			}else {
				req.setAttribute("msg", "*请输入正确的验证码");
				req.getRequestDispatcher("regist.jsp").forward(req, resp);
			}
		}else {
			req.setAttribute("msg1", "*用户名已存在");
			req.getRequestDispatcher("regist.jsp").forward(req, resp);
		}
		
	}
	
	
}
